package com.avicit.zipkinkafkaesserver.service;

import com.avicit.zipkinkafkaesserver.dao.es.ZipkinInterfaceDurationEsDao;
import com.avicit.zipkinkafkaesserver.entity.ZipkinInterfaceDuration;
import com.avicit.zipkinkafkaesserver.enums.ESIndexsEnum;
import com.avicit.zipkinkafkaesserver.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import scala.Tuple2;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 接口耗时列表统计
 */
@Slf4j
@Service
public class ZipkinInterfaceDurationService {

    @Autowired
    private ZipkinInterfaceDurationEsDao zipkinInterfaceDurationEsDao;


    public boolean save(String index, String type,List<ZipkinInterfaceDuration> tuple2List) throws IOException {
        return zipkinInterfaceDurationEsDao.save(index,type, tuple2List);
    }

    /**
     * 将 spark 计算的结果保存到es
     * @param tuple2List
     * @return
     */
    public boolean save(List<Tuple2<String, Double>>tuple2List) {
        try {
            List<ZipkinInterfaceDuration> countVos = tuple2List.stream()
                    .map(tuple2 -> {
                        String[] split = tuple2._1.split("@");
                        return ZipkinInterfaceDuration.builder()
//                                .id(IDFactory.uuid())
                                .interfaceIp(split[0])//取ip
                                .serverName(split[1])//取服务名
                                .interfaceMethod(split[2])//请求方法
                                .interfacePath(split[3])//取接口路径
//                                .createTime(Long.valueOf(split[4]))//取创建时间
                                .interfaceDuration(tuple2._2)//设置接口耗时
                                .queryKey(new StringBuffer(split[0]).append("@")
                                        .append(split[1]).append("@")
                                        .append(split[2]).append("@")
                                        .append(split[3])
                                        .toString()
                                ).build();
                    })
                    .collect(Collectors.toList());

            //批量写入es
            String indexPrefix = ESIndexsEnum.ZipkinInterfaceDuration.getIndexPrefix();
            String index = new StringBuffer(indexPrefix).append("-").append(TimeUtils.nowDateString()).toString();
            boolean save = this.save(index, indexPrefix, countVos);
            if (!save) log.error("save ZipkinInterfaceDuration to es error :{}",false);
            return save;
        }catch (Exception e) {
            log.error("save ZipkinInterfaceCountList to es error :{}",e);
            return false;
        }
    }

    /**
     * 接口耗时查询
     * topN
     * min/max
     * @return
     */
    public List<ZipkinInterfaceDuration> query(long startTime, long endTime, long min, long max, int topN) {
        String indexPrefix = ESIndexsEnum.ZipkinInterfaceDuration.getIndexPrefix();
        String index = new StringBuffer(indexPrefix).append("-").append(TimeUtils.nowDateString()).toString();
        return zipkinInterfaceDurationEsDao.query(startTime, endTime, min, max,topN, index, indexPrefix);
    }

    public List<ZipkinInterfaceDuration> pecentQuery(long startTime, long endTime, long min, long max, int topN) {
        String indexPrefix = ESIndexsEnum.ZipkinInterfaceDuration.getIndexPrefix();
        String index = new StringBuffer(indexPrefix).append("-").append(TimeUtils.nowDateString()).toString();
        return zipkinInterfaceDurationEsDao.pecentQuery(startTime, endTime, min, max,topN, index, indexPrefix);
    }
}
